//
//  MainViewController.m
//  ProjectA_V0.1
//
//  Created by liuchang on 14/12/29.
//  Copyright (c) 2014年 liuchang. All rights reserved.
//

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "MainViewController.h"
#import "NormalTableViewCell.h"
#import "TimingTableViewCell.h"
#import "NormalClockModel.h"
#import "NormalDataBase.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.clockArray = [NSMutableArray array];
        [self setUI];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addRecognizer];
}

- (void)setUI
{
    NSArray * normal = [NormalDataBase searchAllModel];
    [_clockArray addObject:normal];
    
    self.mainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_mainView setDataSource:self];
    [_mainView setDelegate:self];
    [self.view addSubview:_mainView];
    [_mainView release];
//    NormalClockModel * model = [[NormalClockModel alloc] init];
//    model.time = @"1231";
//    model.isOn = YES;
//    model.remark = @"test";
//    [NormalDataBase insertIntoActWithModel:model];
}

#pragma mark - tableViewProtocol

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_clockArray objectAtIndex:section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_clockArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString * norCellIdentify = @"norCell";
        NormalTableViewCell * norCell = [tableView dequeueReusableCellWithIdentifier:norCellIdentify];
        if (!norCell) {
            norCell = [[NormalTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:norCellIdentify];
        }
        NormalClockModel * p = [[_clockArray firstObject] objectAtIndex:indexPath.row];
        return norCell;
    }
    static NSString * timCellIdentify = @"timCell";
    TimingTableViewCell * timCell = [tableView dequeueReusableCellWithIdentifier:timCellIdentify];
    if (!timCell) {
        timCell = [[TimingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:timCellIdentify];
    }
    return timCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark - function

- (void)buttonAction:(id)sender
{
    NSLog(@"hahaha");
}

- (void)addRecognizer
{
    UIScreenEdgePanGestureRecognizer * panRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerAction:)];
    panRecognizer.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:panRecognizer];
    [panRecognizer release];
    
    UIScreenEdgePanGestureRecognizer * rightRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(rightRecognizerAction:)];
    rightRecognizer.edges = UIRectEdgeRight;
    [self.view addGestureRecognizer:rightRecognizer];
    [rightRecognizer release];
}

- (void)recognizerAction:(UIScreenEdgePanGestureRecognizer *)recognizer
{
    if (!_sideIsShow) {
        self.leftBlock(recognizer);
    }
}

- (void)rightRecognizerAction:(UIScreenEdgePanGestureRecognizer *)recognizer
{
    if (!_sideIsShow) {
        self.rightBlock(recognizer);
    }
}

- (void)leftGetBlock:(LeftViewBlock)leftBlock
{
    self.leftBlock = leftBlock;
}

- (void)rightGetBlock:(RightViewBlock)rightBlock
{
    self.rightBlock = rightBlock;
}
@end
